Skip to content

opentelemetry-instrumentation-httpx: support strict typing#4833

Open
henry3260 wants to merge 2 commits into
open-telemetry:mainfrom
henry3260:httpx-strict-typing
Open

opentelemetry-instrumentation-httpx: support strict typing#4833
henry3260 wants to merge 2 commits into
open-telemetry:mainfrom
henry3260:httpx-strict-typing

Conversation

@henry3260

Copy link
Copy Markdown
Contributor

Description

Add opentelemetry-instrumentation-httpx to the pyright strict typecheck
environment (tox.ini typecheck deps + pyright include in pyproject.toml)
and fix the typing across the instrumentation. Follow-up to the review
discussion in #4730.

Notable changes, none of which are intended to change runtime behavior:

  • Restructure the isinstance dispatch in _extract_response (check tuple
    first) so pyright narrowing works soundly in both branches; this removes the
    previously flagged unnecessary typing.cast calls.
  • Declare the transport handle_request/handle_async_request methods as
    returning httpx.Response to be compatible with the httpx.BaseTransport
    override; the httpx < 0.20 tuple form is still handled at runtime.
  • Dispatch sync/async hooks in instrument_client via TypeIs guards
    (pyright does not narrow callable unions in the negative direction, so a
    pair of positive guards is used).
  • Type span/metric attribute dicts as dict[str, AttributeValue] and make the
    wrapper histogram/hook parameters optional to match call sites.
  • Widen RequestInfo/ResponseInfo url and extensions field annotations
    to match the raw values the transport layer actually passes.

Remaining suppressions: two typing.cast calls at the transport returns
(control-flow invariant pyright cannot see, documented inline) and two
targeted pyright: ignore comments for untyped wrapt and the deprecated
MetricInstruments name required by the old semconv mode — both follow
existing precedent in already-typechecked packages.

Package tests remain excluded from typecheck for now, consistent with the
other packages in the include list.

Fixes #4818

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • tox -e typecheck — 0 errors for the package
  • tox -e lint-instrumentation-httpx — 10.00/10
  • tox -e py312-test-instrumentation-httpx-{0,1,2,3}-wrapt2,
    py312-test-instrumentation-httpx-1-wrapt1, and
    py310-test-instrumentation-httpx-1-wrapt2 — all pass; the -0 factor
    (httpx 0.18) exercises the legacy tuple code path touched by the
    _extract_response restructuring

Does This PR Require a Core Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added (typing-only change; covered by the existing test suite)
  • Documentation has been updated (not applicable)

@henry3260
henry3260 requested a review from a team as a code owner July 18, 2026 01:23
@henry3260
henry3260 force-pushed the httpx-strict-typing branch from ed0be17 to 2a97a2c Compare July 18, 2026 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentor-httpx: support strict typing

1 participant